//Detail模块的小仓库
import {reqGetDetailInfo,reqAddOrUpdateShopCart} from '@/api'
// 封装临时游客uuid
import {getUUID} from '@/utils/uuid_token'
// actions 处理异步，接受服务器端的数据
const actions = {
  async getgoodInfo(context,skuId){
    let result = await reqGetDetailInfo(skuId)
      if(result.code==200){
        context.commit('GETGOODINFO',result.data)
    }
  },
  // 将产品加入到购物车中
  async AddOrUpdateShopCart(context,{skuId,skuNum}){
    // 购物车返回给前端
    // 服务器写入数据成功，加入购物车并不需要返回其他数据，所以只是返回code=200，代表本次成功
    let result = await reqAddOrUpdateShopCart(skuId,skuNum)
      if(result.code==200){
        console.log(result)
      }else{
        return Promise.reject(new Error('faile'))
      }
   }
      
}
//mutations 修改state的唯一方法
const mutations = {
  GETGOODINFO(state,goodInfo){
    state.goodInfo = goodInfo
  },
  
}
//state仓库存储数据
const state = {
  goodInfo:{},
  uuid_token:getUUID()
}
// getters 计算属性，组件获取仓库的数据更加方便
const getters = {
  categoryView(state){
    return state.goodInfo.categoryView||{}
  },
  skuInfo(state){
    return state.goodInfo.skuInfo||{}
  },
  spuSaleAttrList(state){
    return state.goodInfo.spuSaleAttrList||{}
  },
}

export default {
    state,
    actions,
    mutations,
    getters,
  };

